package com.me.Helpers;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;
import com.me.GameObjects.Bubble;
import com.me.GameWorld.GameRenderer;
import com.me.GameWorld.GameWorld;

public class InputHandler implements InputProcessor, GestureListener {
	
	private GameWorld myWorld;
	private GameRenderer myRenderer;
	
	public InputHandler(GameWorld gw, GameRenderer gr) {
		this.myWorld=gw;
		this.myRenderer = gr;
		
		
	}

	//-------------------------------
	// metodos del InputProcessor
	//-------------------------------

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {

		//    	myRenderer.getCam().position.y = screenY;
		//    	myRenderer.getCam().position.x = screenX;


		//    	myWorld.getGrid().getPosition().x=screenX;
		//    	myWorld.getGrid().getPosition().y=screenY;



		return false;
	}

	@Override
	public boolean keyDown(int keycode) {
		
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
//		System.out.println("touch up");
		//myWorld.getGrid().cambiarEstado(screenX, screenY, Bubble.INFLADO);
		return true;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
//		System.out.println("touch dragged");
		return true;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		//    	float z = myRenderer.getCam().zoom;
		//    	if(z+amount<1)myRenderer.getCam().zoom =1;
		//    	else if(z+amount>100)myRenderer.getCam().zoom=100;
		//    	else	myRenderer.getCam().zoom += amount;

//		System.out.println("scrolled performed " +amount);
		return true;
	}


	//-------------------------------
	// metodos del gestureListener
	//-------------------------------

	@Override
	public boolean touchDown(float x, float y, int pointer, int button) {
		
		myRenderer.updateScale();

		    	//System.out.println("Touch down "+x+","+y);	    	
		    	
		    	float px=(myRenderer.getposCam().x+(x*myRenderer.getCam().zoom));
		    	float py=(myRenderer.getposCam().y+(y*myRenderer.getCam().zoom));
		    	
		    	//System.out.println("cam "+myRenderer.getposCam().x+","+myRenderer.getposCam().y);	   
			    	
		    	//System.out.println("#######Touch down "+px+","+py+"##"+myRenderer.getCam().zoom);
		myWorld.cambiarEstado(px, py, Bubble.ESTRIPADO);


		return true;
	}

	@Override
	public boolean tap(float x, float y, int count, int button) {
		String message = "Tap performed, finger" + Integer.toString(button);
		//        System.out.println(message);
		float px=(myRenderer.getposCam().x+(x*myRenderer.getCam().zoom));
    	float py=(myRenderer.getposCam().y+(y*myRenderer.getCam().zoom));
    	
		myWorld.cambiarEstado(px, py, Bubble.INFLADO);
		//System.out.println("d"+count);
		if( count>=10 ){
			myWorld.createGrid(myWorld.getGrid().getWidth(),myWorld.getGrid().getHeight(), myWorld.getGrid().getPosition(),50);
		}
		return true;
	}

	@Override
	public boolean longPress(float x, float y) {
		String message = "Long press performed";
		//        System.out.println(message);
		float px=(myRenderer.getposCam().x+(x*myRenderer.getCam().zoom));
    	float py=(myRenderer.getposCam().y+(y*myRenderer.getCam().zoom));
    	
		myWorld.cambiarEstado(px, py, Bubble.EXPLOTADO);

		return true;
	}

	@Override
	public boolean fling(float velocityX, float velocityY, int button) {
		String message = "Fling performed, velocity:" + Float.toString(velocityX) +
				"," + Float.toString(velocityY);
		//        System.out.println(message);

		return true;
	}

	@Override
	public boolean pan(float x, float y, float deltaX, float deltaY) {
		String message = "Pan performed, delta:" + Float.toString(deltaX) +
				"," + Float.toString(deltaY);
		//        System.out.println(message);
		float px=(myRenderer.getposCam().x+(x*myRenderer.getCam().zoom));
    	float py=(myRenderer.getposCam().y+(y*myRenderer.getCam().zoom));
    	
		myWorld.cambiarEstado(px, py, Bubble.EXPLOTADO);
		return true;
	}

	@Override
	public boolean zoom(float initialDistance, float distance) {
		
		//myRenderer.zoomCam(initialDistance,distance);

		return true;
	}

	@Override
	public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
			Vector2 pointer1, Vector2 pointer2) {
     		myRenderer.movCam2(initialPointer1,initialPointer2,pointer1,pointer2);
		return true;
	}

	@Override
	public boolean panStop(float x, float y, int pointer, int button) {
		String message = "Pan STOP performed, pointer:" + pointer +
				", button " + button;
		        System.out.println(message);
		return true;
	}




}
